home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / general / GUIController.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  5.5 KB  |  186 lines

  1. " --------------------------------------------------------------------
  2. * The GUIController Class allows the User (& Models) to interface with
  3. * a view of some other Objects (Usually Data Entry Gadgets).
  4. *
  5. * This class is NOT finished!
  6. * -------------------------------------------------------------------- 
  7. "
  8. Class GUIController :Controller ! guiWindow ctrlDictionary !
  9. [
  10.    new
  11.       
  12.       super new.
  13.       
  14.       ctrlDictionary <- Dictionary new.
  15.       
  16.       ^ self
  17. |
  18.    startUp
  19.  
  20.       " Give control to the receiver.  The default control 
  21.       * sequence is to initialize (see Controller/controlInitialize), 
  22.       * to loop (see Controller|controlLoop), and then to terminate 
  23.       * (see Controller|controlTerminate).  After this sequence, 
  24.       * control is returned to the sender of Control|startUp. 
  25.       * The receiver's control sequence is used to coordinate the 
  26.       * interaction of its view and model.  In general, this consists 
  27.       * of polling the sensor for user input, testing the 
  28.       * input with respect to the current display of the view,
  29.       * and updating the model to reflect intended changes.
  30.       "
  31.       self controlInitialize.
  32.       self controlLoop.
  33.       self controlTerminate
  34. |
  35.    terminateAndInitializeAround: aBlock
  36.  
  37.       self controlTerminate.
  38.  
  39.       aBlock value.
  40.  
  41.       self controlInitialize
  42. |
  43.    controlInitialize
  44.  
  45.       " Sent by 'startUp' as part of the standard control sequence, it 
  46.       * provides a place in the standard control sequence for 
  47.       * initializing the receiver (taking into account the current
  48.       * state of its model and view).
  49.       "
  50.       super initialize.
  51. |
  52.    controlLoop ! notDone userData !
  53.  
  54.       " Sent by startUp as part of the standard control sequence. 
  55.       * As long as false is NOT returned, the loop continues. 
  56.       * When false is returned, the loop ends:
  57.       "
  58.       notDone <- true.
  59.       
  60.       [notDone ~= false]
  61.         whileTrue: [ userData <- self checkForUserData.
  62.  
  63.                      (userData notNil)
  64.                         ifTrue: [notDone <- self decode: userData].
  65.  
  66.                      " Somehow, we should poll the models for any
  67.                      * changes right here:
  68.                      "
  69.                      (super model hasUnacceptedEdits)
  70.                         ifTrue: [ super refreshView ]
  71.                    ].
  72.  
  73.       ^ false  " Inform everyone that we are done "
  74. |
  75.    controlTerminate   ! ele controlObj !
  76.       
  77.       " User did something to exit the Controller Loop.
  78.       * Provide a place in the standard control sequence for 
  79.       * terminating the receiver (taking into account the 
  80.       * current state of its model and view).
  81.       "
  82.       ele <- ctrlDictionary first.
  83.       
  84.       [ele notNil]
  85.          whileTrue: [ controlObj <- ctrlDictionary at: ele.
  86.                       
  87.                       controlObj dispose.
  88.                                              
  89.                       ctrlDictionary removeKey: ele ifAbsent: [ ^ nil ].
  90.  
  91.                       ele <- ctrlDictionary next. 
  92.                     ].
  93.       ^ nil
  94. |
  95.    addControl: controlObject named: ctrlID
  96.  
  97.       ctrlDictionary at: ctrlID put: controlObject.
  98.  
  99.       controlObject registerTo: guiWindow
  100. |
  101.    addHotKey: keyValue to: controlObject
  102.  
  103.       " Upper-case & lower-case keys are equivalent here: " 
  104.       <primitive 239 3 12 keyValue controlObject>
  105. |
  106.    addMenuSelection: menuObject named: menuID
  107.  
  108.       ctrlDictionary at: menuID put: menuObject
  109.  
  110.       menuObject registerTo: guiWindow
  111. |
  112.    addMenuHotKey: keyValue to: menuObject
  113.  
  114.       <primitive 239 1 12 keyValue menuObject>
  115. |
  116.    checkForUserData
  117.  
  118.       " userData is an Array of three elements, first is the 
  119.       * Gadget (controlObject) value, second is the userData 
  120.       * (a #methodSymbol for this Class), third is the hotKey value.
  121.       *
  122.       * If the User selected a menu item, the first element is
  123.       * the menuitem userData, second is the menuitem Label, 
  124.       * third is Command Key equivalent (a through z, 0 through 9, 
  125.       * or A through Z only [for now!]) 
  126.       * If the User did NOT generate a usable IDCMP event,
  127.       * this method will return nil.
  128.       "
  129.       ^ <primitive 239 3 19 guiWindow>
  130. |
  131.    closeGUIWindow
  132.  
  133.       " <primitive 181 0 guiWindow>. "
  134.       
  135.       ^ false
  136. |
  137.    rawKey: keyCode
  138.  
  139.       ^ true
  140. |
  141.    vanillaKey: keyCode
  142.  
  143.       ^ true
  144. |
  145.    newSizeWindow
  146.  
  147.       ^ true
  148. |
  149.    changeWindow
  150.  
  151.       ^ true
  152. |
  153.    decode: controlData ! ctrlType ctrlID ctrlSymbol hotKey ctrlValue !
  154.  
  155.       ctrlType   <- controlData at: 1. " Integer, String or nil  "
  156.       ctrlID     <- controlData at: 2. " String or Integer       "
  157.       ctrlSymbol <- controlData at: 3. " Usually a Symbol        "
  158.       hotKey     <- controlData at: 4. " Raw, Vanilla key or nil "
  159.       ctrlValue  <- controlData at: 5. " String, Integer or nil  "
  160.  
  161.       (ctrlSymbol = #closeWindow)
  162.          ifTrue: [ ^ self closeGUIWindow ].
  163.          
  164.       (ctrlSymbol = #rawKey)
  165.          ifTrue: [ ^ self rawKey: hotKey ].
  166.                   
  167.       (ctrlSymbol = #vanillaKey)
  168.          ifTrue: [ ^ self vanillaKey: hotKey ].
  169.                   
  170.       (ctrlSymbol = #newSizeWindow)
  171.          ifTrue: [ ^ self newSizeWindow ].
  172.                   
  173.       (ctrlSymbol = #changeWindow)
  174.          ifTrue: [ ^ self changeWindow ].
  175.                   
  176.       ^ (ctrlDictionary at: ctrlID) perform: ctrlSymbol
  177. |
  178.    testNewMenuUserData ! rval !
  179.  
  180.       rval <- self waitForUserData.
  181.       
  182.       ('menu userData[1] is: ', (rval at: 1) asString) print.
  183.       ('menu userData[2] is: ', (rval at: 2) asString) print.
  184.       ('menu userData[3] is: ', (rval at: 3) asString) print
  185. ]
  186.